package com.lingxu.module.earlyWarn.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lingxu.base.common.api.vo.Result;
import com.lingxu.module.collaborative.entity.Collaborative;
import com.lingxu.module.collaborative.mapper.CollaborativeMapper;
import com.lingxu.module.collaborative.service.CollaborativeService;
import com.lingxu.module.earlyWarn.mapper.CockpitMapper;
import com.lingxu.module.earlyWarn.service.CockpitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

/**
 * @Description: 驾驶舱
 * @Author: jfx
 * @Date:   2023-04-11
 * @Version: V1.0.1
 */
@Service
public class CockpitServiceImpl extends ServiceImpl<CockpitMapper, Collaborative> implements CockpitService {

    @Autowired
    private CockpitMapper cockpitMapper;

    @Override
    public Result warningSituation(String region) {
       Map<String,String> info = cockpitMapper.warningSituation(region);
        return Result.ok(info,"查询成功");
    }

    @Override
    public Result warningFieldDistribution(String region) {
        List<Map<String,String>> info = cockpitMapper.warningFieldDistribution(region);
        return Result.ok(info,"查询成功");
    }

    @Override
    public Result warningAreaDistribution(String region) {
        List<Map<String,String>> info = cockpitMapper.warningAreaDistribution(region);
        List<Map<String,String>> city = cockpitMapper.getCityList();
        boolean flag = true;
        for (int i = 0; i < city.size(); i++) {
            for (int j = 0; j < info.size(); j++) {
                if (city.get(i).get("ID").substring(0,4).equals(info.get(j).get("REGION"))){
                    city.get(i).put("COUNTS",info.get(j).get("COUNTS"));
                    flag = false;
                }
            }
            if (flag){
                city.get(i).put("COUNTS","0");
            }else {
                flag = true;
            }
        }
        return Result.ok(city,"查询成功");
    }

    @Override
    public Result highFrequencyIssues(String region) {
        List<Map<String,String>> info = cockpitMapper.highFrequencyIssues(region);
        return Result.ok(info,"查询成功");
    }

    @Override
    public Result findModelIssueList(String region) {
        List<Map<String,String>> info = cockpitMapper.findModelIssueList(region);
        return Result.ok(info,"查询成功");
    }

    @Override
    public Result modelTypeDistribution(String year) {
        List<Map<String,String>> info = cockpitMapper.modelTypeDistribution(year);
        return Result.ok(info,"查询成功");
    }

    @Override
    public Result modelAreaDistribution(String year) {
        List<Map<String,String>> info = cockpitMapper.modelAreaDistribution(year);
        List<Map<String,String>> city = cockpitMapper.getCityList();
        boolean flag = true;
        for (int i = 0; i < city.size(); i++) {
            for (int j = 0; j < info.size(); j++) {
                if (city.get(i).get("ID").substring(0,4).equals(info.get(j).get("REGION"))){
                    city.get(i).put("COUNTS",info.get(j).get("COUNTS"));
                    flag = false;
                }
            }
            if (flag){
                city.get(i).put("COUNTS","0");
            }else {
                flag = true;
            }
        }
        return Result.ok(city,"查询成功");
    }

    @Override
    public Result timeTrend(String year) {
        if (null == year || "".equals(year)){
            // 获取当前日期
            LocalDate currentDate = LocalDate.now();
            // 提取年份
            year = currentDate.getYear()+"";
        }
        List<Map<String,String>> info = cockpitMapper.timeTrend(year);
        List<Map<String,String>> infos = new ArrayList<>();
                List<String> Yue = new ArrayList<>();
        Map<String,String> mapY = new HashMap<>();
        Yue.add("01");Yue.add("02");Yue.add("03");Yue.add("04");Yue.add("05");Yue.add("06");Yue.add("07");Yue.add("08");Yue.add("09");Yue.add("10");Yue.add("11");Yue.add("12");

        Map<String,String> map = new HashMap<>();
        boolean flag = true;
        for (int i = 0; i < Yue.size(); i++) {
            for (int j = 0; j < info.size(); j++) {
                if (Yue.get(i).equals(info.get(j).get("MONTH"))){
                    map.put("COUNTS",info.get(j).get("COUNTS"));
                    map.put("MONTH",Yue.get(i));
                    infos.add(map);
                    flag = false;
                }
            }
            if (flag){
                map.put("MONTH",Yue.get(i));
                map.put("COUNTS","0");
                infos.add(map);
            }else {
                flag = true;
            }
            map = new HashMap<>();
        }
        return Result.ok(infos,"查询成功");
    }

    @Override
    public Result modelfieldDistribution(String year) {
        if (null == year || "".equals(year)){
            // 获取当前日期
            LocalDate currentDate = LocalDate.now();
            // 提取年份
            year = currentDate.getYear()+"";
        }
        List<Map<String,String>> info = cockpitMapper.modelfieldDistribution(year);
        return Result.ok(info,"查询成功");
    }

    @Override
    public Result modelList(String year) {
        if (null == year || "".equals(year)){
            // 获取当前日期
            LocalDate currentDate = LocalDate.now();
            // 提取年份
            year = currentDate.getYear()+"";
        }
        List<Map<String,String>> info = cockpitMapper.modelList(year);
        return Result.ok(info,"查询成功");
    }

    @Override
    public Result realTimeWarning(String year) {
        if (null == year || "".equals(year)){
            // 获取当前日期
            LocalDate currentDate = LocalDate.now();
            // 提取年份
            year = currentDate.getYear()+"";
        }
        List<Map<String,String>> info = cockpitMapper.realTimeWarning(year);
        return Result.ok(info,"查询成功");
    }

    @Override
    public Result townshipRanking(String year) {
        if (null == year || "".equals(year)){
            // 获取当前日期
            LocalDate currentDate = LocalDate.now();
            // 提取年份
            year = currentDate.getYear()+"";
        }
        List<Map<String,String>> info = cockpitMapper.townshipRanking(year);
        return Result.ok(info,"查询成功");
    }

    @Override
    public Result compareCounts(String year) {
        String month = "01";
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        if (null == year || "".equals(year)){
            // 提取年份
            year = currentDate.getYear()+"";
        }
        // 提取月份
        month = currentDate.getMonthValue()+"";
        if (month.length()==1){
            month = "0"+month;
        }
        List<Map<String,String>> info = cockpitMapper.compareCounts(year,month);
        return Result.ok(info,"查询成功");
    }

    @Override
    public Result listenResults(String region) {
        Map<String,String> info = cockpitMapper.listenResults(region);
        if (null == info.get("ZDCXS")){
            info.put("ZDCXS","0");
        }
        if (null == info.get("CLRC")){
            info.put("CLRC","0");
        }
        if (null == info.get("WHSS")){
            info.put("WHSS","0");
        }
        if (null == info.get("DXAL")){
            info.put("DXAL","0");
        }
        if (null == info.get("JDMX")){
            info.put("JDMX","0");
        }
        List<Map<String,String>> list = cockpitMapper.findYbsList(region);
        int sfYbs = 0;
        for (int i = 0; i < list.size(); i++) {
            // 创建 Gson 对象
            Gson gson = new Gson();

            // 将 JSON 数组转换为 List<Map<String, String>>
            Type listType = new TypeToken<List<Map<String, String>>>(){}.getType();
            List<java.util.Map<String, String>> dataList = gson.fromJson(list.get(i).get("CASEINFODETAILS"), listType);
            // 提取数据
            for (java.util.Map<String, String> data : dataList) {
                String disposalYbs = data.get("disposalYbs");
                if (disposalYbs.equals("1")){
                    sfYbs++;
                }
            }
        }
        info.put("YBSCLRS",sfYbs+"");
        return Result.ok(info,"查询成功");
    }
}
